package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type OperateManualTaskEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	TaskName     string // 模板名称
	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名
	DeviceName   string // 设备类型名

	TaskParam []map[string]interface{} // 模板参数
}

type operateManualTaskPo struct {
	Id           int64                    `json:"id" db:"id"`                     // 对象id
	CreateTime   int64                    `json:"createTime" db:"create_time"`    // 创建时间
	UpdateTime   int64                    `json:"updateTime" db:"update_time"`    // 更新时间
	Manufacturer string                   `json:"manufacturer" db:"manufacturer"` // 设备厂商名称
	DeviceType   string                   `json:"deviceType" db:"device_type"`    // 设备类型名
	DeviceName   string                   `json:"deviceName" db:"device_name"`    // 设备类型名
	TaskName     string                   `json:"taskName" db:"task_name"`        // 设备名称
	TaskParam    []map[string]interface{} `json:"taskParam" db:"task_param"`      // 配置参数
}

func (e *OperateManualTaskEntity) Type() string {
	return "OperateManualTaskEntity"
}

func (e *OperateManualTaskEntity) TableName() string {
	return "tb_operate_manual_task"
}

func (e *OperateManualTaskEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *OperateManualTaskEntity) Instance() *OperateManualTaskEntity {
	return &OperateManualTaskEntity{
		BaseEntity: new(commEntity.BaseEntity),
		TaskParam:  make([]map[string]interface{}, 0),
	}
}

func (e *OperateManualTaskEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *OperateManualTaskEntity) NewPo() interface{} {
	return &operateManualTaskPo{
		TaskParam: make([]map[string]interface{}, 0),
	}
}

func (e *OperateManualTaskEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *OperateManualTaskEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.TaskName)
	return p
}

func (e *OperateManualTaskEntity) BuildByJson(jsn *string) error {
	var po operateManualTaskPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *OperateManualTaskEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *OperateManualTaskEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*operateManualTaskPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.TaskParam = po.TaskParam
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.DeviceName = po.DeviceName
	ptr.TaskName = po.TaskName
}

func (e *OperateManualTaskEntity) BuildToPo() interface{} {
	ptr := e
	po := operateManualTaskPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.TaskParam = ptr.TaskParam
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.DeviceName = ptr.DeviceName
	po.TaskName = ptr.TaskName

	return &po
}
